<?php

namespace App\Services\Index;


use App\Dao\PayCallDao;
use App\Model\PayCall;
use App\Model\UserToken;
use App\Utils\MemcacheClient;

class FrontBackService
{

    /**
     * Note - 前后台切换
     * DateTime 2024/8/20 19:01
     * User Admin
     * @param  $uid
     * @param  $params
     * @return array
     */
    public function switchFrontBack($uid, $params)
    {
        $token = $params['token'] ?? '';
        $um_token = $params['um_token'] ?? '';
        $status = intval($params["status"]); // int 当切换到后台或者直接杀进程, 请求status=1；当切回到前台，请求status=0
        $package = trim($params["package"] ?? '');
        $version_code = trim($params["version_code"] ?? 0);
        $from = trim($params["from"] ?? 1);
        $session_id = $params['session_id'];

        if (!empty($um_token)) {
            $data["from"] = intval($from);
            $data["device_token"] = $token;
            $data["um_token"] = $um_token;
            $data["status"] = $status;
            $data["update_time"] = time();
            $data["package"] = $package;

            $rs = UserToken::query()->where('uid', $uid)->first();
            if (!empty($rs)) {
                UserToken::query()->where('uid', $uid)->update($data);
            } else {
                $data['uid'] = $uid;
                $data["version_code"] = $version_code;
                $data["ctime"] = time();
                $lastId = UserToken::query()->insertGetId($data);
            }
        }
        // 切回到前台
        if ($status == 1) {
            $payCallModel = PayCallDao::getInstance();
            $call_data = MemcacheClient::getInstance()->get('anchor_call_mass_list_' . 1 . '_' . $uid);
            if (!empty($call_data)) {
                $payCallModel->massRefuse($uid, $session_id, 0, $call_data, $uid, 1);
            }
            $call_data = MemcacheClient::getInstance()->get('anchor_call_mass_list_' . 2 . '_' . $uid);
            if (!empty($call_data)) {
                $payCallModel->massRefuse($uid, $session_id, 0, $call_data, $uid, 2);
            }
        }
        // Tool::JSON(\CodeData::SUCCESS, array('result' => 1)); //成功
        return formatData(['result' => 1]);
    }
}